#include <stdio.h>
#include <fcntl.h>   /* File Control Definitions           */
#include <termios.h> /* POSIX Terminal Control Definitions */
#include <unistd.h>  /* UNIX Standard Definitions 	   */ 
#include <errno.h>   /* ERROR Number Definitions           */

#include <string.h>   /* ERROR Number Definitions           */
#include "SerialPort_write.h"   /* ERROR Number Definitions           */

int fd;/*File Descriptor*/

void open_serial(void)
{	
	fd = open(SerialPort_Num,O_RDWR | O_NOCTTY);	

	if(fd==-1)
	{
    	   	printf("\n  Error! in Opening ttyUSB0  ");
	}
	else
	{
        //printf("\n  ttyUSB0 Opened Successfully ");


		/*---------- Setting the Attributes of the serial port using termios structure --------- */
	
		struct termios SerialPortSettings;	/* Create the structure                          */

		tcgetattr(fd, &SerialPortSettings);	/* Get the current attributes of the Serial port */

		cfsetispeed(&SerialPortSettings,SerialPort_BaudRate); /* Set Read  Speed as 9600                       */
		cfsetospeed(&SerialPortSettings,SerialPort_BaudRate); /* Set Write Speed as 9600                       */

		SerialPortSettings.c_cflag &= ~PARENB;   /* Disables the Parity Enable bit(PARENB),So No Parity   */
		SerialPortSettings.c_cflag &= ~CSTOPB;   /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
		SerialPortSettings.c_cflag &= ~CSIZE;	 /* Clears the mask for setting the data size             */
		SerialPortSettings.c_cflag |=  CS8;      /* Set the data bits = 8                                 */

		SerialPortSettings.c_cflag &= ~CRTSCTS;       /* No Hardware flow Control                         */
		SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines       */ 
	
	
		SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);          /* Disable XON/XOFF flow control both i/p and o/p */
		SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);  /* Non Cannonical mode                            */

		SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/

        SerialPortSettings.c_cc[VMIN] = 0;/* Read at least 10 characters */
        SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly   */

		if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/
		    printf("\n  ERROR ! in Setting attributes");
		else
            ;//printf("\n  BaudRate = 115200 \n  StopBits = 1 \n  Parity   = none");
	}

}
void close_serial(void)
{
	close(fd);/* Close the Serial port */
}
void send_data(const unsigned char *buffer,unsigned char len)
{
	/*------------------------------- Write data to serial port -----------------------------*/

	int  bytes_written  = 0;  	/* Value for storing the number of bytes written to the port */ 

	open_serial();

	bytes_written = write(fd,buffer,len);			     /* use write() to send data to port                                            */
								     /* "fd"                   - file descriptor pointing to the opened serial port */
								     /*	"write_buffer"         - address of the buffer containing data	            */
								     /* "sizeof(write_buffer)" - No of bytes to write                               */	
        //printf("\n  %s written to ttyUSB0",buffer);
        //printf("\n  %d Bytes written to ttyUSB0", bytes_written);
        //printf("\n +----------------------------------+\n\n");
        close_serial();
}
int read_data(char *read_buffe)
{
	//open_serial();

	//tcflush(fd, TCIFLUSH);   /* Discards old data in the rx buffer            */

    //char read_buffer[1];   /* Buffer to store the data received              */
    //int  bytes_read = 0;    /* Number of bytes read by the read() system call */
    //int i = 0;
    int num=0;

    num = read(fd,read_buffe,1); /* Read the data                   */
		
	//printf("\n\n  Bytes Rxed -%d", bytes_read); /* Print the number of bytes read */
	//printf("\n\n  ");

    //for(i=0;i<num;i++)	 /*printing only the received characters*/
    //    printf("%c",read_buffe[i]);
    //if(num)
    //    printf("%c",*read_buffe);

    if(num==1)
        return 1;
    else
        return 0;

	//printf("\n +----------------------------------+\n\n\n");

	//close_serial();
}

